package cn.luckdou.system.controller.oss_file;


import java.util.Date;

import cn.luckdou.commons.utils.AliyunOSSClientUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import cn.luckdou.commons.base.BaseController;
import cn.luckdou.commons.result.PageInfo;
import cn.luckdou.system.model.oss_file.OssFile;
import cn.luckdou.system.service.oss_file.IOssFileService;
import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;

/**
 * OSS文件管理
 * @author Dadou
 * @DateTime 2018-06-05 
 **/
@Controller
@RequestMapping("/admin/ossFile")
public class OssFileController extends BaseController {
	// log日志
	private static Logger logger = Logger.getLogger(OssFileController.class);
    
    @Autowired private IOssFileService ossFileService;
    
    /**
	 * 列表页面
     */
	@RequiresPermissions("/admin/ossFile/manager")
    @GetMapping("/manager")
    public String manager() {
        return "admin/system/ossFile/ossFileList";
    }

	/**
	 * 列表数据
     */
	@RequiresPermissions("/admin/ossFile/manager")
    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(OssFile ossFile, Integer page, Integer rows, String sort,String order) {
        EntityWrapper<OssFile> wrapper = new EntityWrapper<OssFile>();
        if (StringUtils.isNotBlank(ossFile.getUrl())){
            wrapper.like("url",ossFile.getUrl());
            ossFile.setUrl(null);
        }
        wrapper.setEntity(ossFile);
        Page<OssFile> pages = getPage(page, rows, sort, order);
        pages = ossFileService.selectPage(pages,wrapper);
        return pageToPageInfo(pages);
    }
    
    /**
	 * 添加页面
     */
	@RequiresPermissions("/admin/ossFile/add")
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/system/ossFile/ossFileAdd";
    }
    
    
    /**
	 * 添加数据
     */
	@RequiresPermissions("/admin/ossFile/add")
    @PostMapping("/add")
    @ResponseBody
    public Object add(OssFile ossFile, String json) {
		ossFile.setCreatedTime(new Date());
		ossFile.setCreatedEditor(this.getShiroUser().getName());
        boolean b = ossFileService.insert(ossFile);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }

    /**
     * 编辑页面
     */
	@RequiresPermissions("/admin/ossFile/edit")
    @GetMapping("/editPage")
    public String editPage(Model model, String id) {
        OssFile ossFile = ossFileService.selectById(id);
        model.addAttribute("ossFile", ossFile);
        return "admin/system/ossFile/ossFileEdit";
    }

    /**
     * 编辑数据
     */
	@RequiresPermissions("/admin/ossFile/edit")
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(OssFile ossFile, String json) {
		ossFile.setUpdatedTime(new Date());
		ossFile.setUpdatedEditor(this.getShiroUser().getName());
        boolean b = ossFileService.updateById(ossFile);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }

	/**
	 * 单个删除
	 */
	@RequiresPermissions("/admin/ossFile/delete")
	@PostMapping("/delete")
	@ResponseBody
	public Object delete(String id) {
        boolean b = ossFileService.deleteById(Long.parseLong(id));
        if (b) {
			return renderSuccess("删除成功！");
        } else {
        	return renderError("删除失败！");
        }
	}

	/**
	 * 批量删除
	 */
	@RequiresPermissions("/admin/ossFile/deleteIds")
	@PostMapping("/deleteIds")
	@ResponseBody
	public Object deleteIds(String ids) {
        if (StringUtils.isNotBlank(ids)) {
			String[] split = ids.split(",");
        	int num = 0;
        	for (String id : split) {
        		boolean b = ossFileService.deleteById(Long.parseLong(id));
        		if (b) num++;
        	}
        	if (num > 0) {
				return renderSuccess("删除"+num+"条记录成功！");
			} else {
				return renderError("删除失败！");
			}
		}
		return renderError("参数错误");
	}

    /**
     * 从OSS中删除
     */
    @PostMapping("/delete2Oss")
    @ResponseBody
    public Object delete2Oss(String id) {
        OssFile ossFile = ossFileService.selectById(Long.parseLong(id));
        try{
            AliyunOSSClientUtil.deleteFile(AliyunOSSClientUtil.getOSSClient(),ossFile.getBucket(),ossFile.getOssKey());
            ossFile.setStatus(0);
            ossFile.setDeletedTime(new Date());
            ossFileService.updateById(ossFile);
            return renderSuccess("删除成功！");
        }catch (Exception e){
            e.printStackTrace();
            return renderError("删除失败！");
        }
    }
    
}
